package com.example.demo.http.controllers;

import com.example.demo.jobs.TestJob;
import com.example.demo.models.admin.Admin;
import com.example.demo.services.system.JWTService;
import com.example.demo.services.system.task.Task;
import com.example.demo.services.system.task.TaskService;
import gaarason.database.eloquent.GeneralModel;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Resource
    GeneralModel generalModel;
//
//    @GetMapping("/test")
//    public String Test() {
//        Record<GeneralModel.Table, Serializable> record = generalModel.newQuery().from("user_data").where("id", 3).firstOrFail();
//
//        // 结果转化到 map
//        Map<String, Object> stringObjectMap = record.toMap();
//
//        System.out.println(stringObjectMap);
//        return stringObjectMap.toString();
//    }

    @GetMapping("/test")
    public String test() {
        Admin admin = new Admin();
        admin.setId(1L);
        String token = JWTService.generateToken(admin);
        TaskService.runTask(new Task("2019095") {
            @Override
            public void run() {
                try {
                    System.out.println("start sleep:" + Thread.currentThread().getName());
                    Thread.sleep(5000);
                    System.out.println("end sleep：" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return token;
        //String ip = (String) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute("ip",RequestAttributes.SCOPE_REQUEST);
        //String user = String.valueOf(Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute("thread",RequestAttributes.SCOPE_REQUEST));
        //return ip+","+user;
    }
}
